import { addCollection } from '@iconify/vue'
import primeIcons from '@iconify-json/prime/icons.json'

import { hasOwnProperty } from '@na/utils'

export function getLocalIcons() {
  const modules = import.meta.glob('./icons/*.svg', { eager: true })
  const localIcons = {
    prefix: 'local',
    icons: {},
    lastModified: 1722793014,
    width: 24,
    height: 24,
  }
  const svgPattern = /data:image\/svg\+xml,|<svg[\s\S]*?>|<\/svg>/gi
  Object.entries(modules).forEach(([path, module]: any) => {
    const name = path.replace('./icons/', '').replace('.svg', '')
    const svgContent = decodeURIComponent(module.default)
    if (svgContent) {
      localIcons.icons[name] = {
        body: svgContent.replace(svgPattern, ''),
      }
      if (hasOwnProperty(primeIcons.icons, name)) {
        delete primeIcons.icons[name]
      }
    }
  })
  Object.assign(localIcons.icons, primeIcons.icons)
  return localIcons
}

export function loadLocalIcons() {
  addCollection(getLocalIcons())
}
